package com.itheima.dao.imp;

import com.itheima.damain.Teacher;
import com.itheima.dao.lnter.ITeacherDao;

import java.util.ArrayList;

public class TeacherDaolmpl implements ITeacherDao {
    private static ArrayList<Teacher> list = new ArrayList<>();

    static {

        Teacher tea1 = new Teacher("001", "张三", 23, "1999-11-11");
        Teacher tea2 = new Teacher("002", "李四", 24, "1888-11-11");
        Teacher tea3 = new Teacher("003", "王五", 25, "1777-11-11");
        list.add(tea1);
        list.add(tea2);
        list.add(tea3);
    }

    @Override // 添加老师
    public void addTeacher(Teacher teacher) {
        list.add(teacher);
    }

    @Override    // 删除老师
    public void deleteTeacher(String sid) {
        int index = getindex(sid);
        list.remove(index);
    }

    @Override   // 修改老师
    public void updateTeacher(String sid, Teacher teacher) {
        int index = getindex(sid);
        list.set(index, teacher);
    }

    // 根据学号判断索引
    private int getindex(String sid) {
        for (int i = 0; i < list.size(); i++) {
            Teacher t = list.get(i);
            if (t != null && t.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }

    @Override   // 查看老师
    public ArrayList<Teacher> findAllTeacher() {
        return list;
    }
}
